package com.datasource.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author fyy
 * @Description 数据源持有类
 * @Date 2020/6/4 10:14
 */
public class DataSourceContextHolder {

    private static final Logger logger = LoggerFactory.getLogger(DataSourceContextHolder.class);
    //线程私有的本地变量
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();


    public static void setDataource(String dbType){
        logger.info("切换数据源到{}",dbType);
        contextHolder.set(dbType);
    }

    public static String getDataSource(){
        return contextHolder.get();
    }

    public static void clearDataSource(){
        contextHolder.remove();
    }

}
